/*
 * @Description: 数据库
 * @Author: iamsmiling
 * @Date: 2021-07-11 13:49:52
 * @LastEditTime: 2021-07-11 16:40:31
 */
const mysql = require("./config");
const Sequelize = require("sequelize");
const fs = require("fs");
const sequelize = new Sequelize(
   mysql.database,
   mysql.username,
   mysql.password,{
       host:mysql.host,
       dialect:"mysql",
       pool:{
            max:5,
            min:0,
            idle: 10000, //如果一个线程 10秒内么有被使用过的话，就释放
         },
      logging: true, // 执行过程会log一些SQL的logging，设为false不显示
   },
);

+  async function(){
   ///初始化
   try {
      await sequelize.authenticate();
      console.log('Connection has been established successfully.');
      let files =  fs.readdirSync(__dirname+"/models")
      for (let f of files) {
         require(__dirname + "/models/" + f);
      }
    } catch (error) {
      console.error('Unable to connect to the database:', error);
    }
}();

module.exports = sequelize
